package com.hmdp.utils;

import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;

import java.util.Collections;
import java.util.List;

public class RedisStream{
    private StringRedisTemplate stringRedisTemplate;

    public RedisStream(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }
    private static final String KEY_PREFIX="stream.";

    private static final DefaultRedisScript<Void> CREATE_STREAM_SCRIPT;
    static {
        CREATE_STREAM_SCRIPT=new DefaultRedisScript<>();
        CREATE_STREAM_SCRIPT.setLocation(new ClassPathResource("createStream.lua"));
        CREATE_STREAM_SCRIPT.setResultType(Void.class);
    }
    public void createStream() {
        // 定义脚本的键（keys）和参数（args）
//        List<String> keys = Collections.singletonList(KEY_PREFIX+name); // 流的键
        // 调用lua脚本
        stringRedisTemplate.execute(CREATE_STREAM_SCRIPT,Collections.emptyList());
//        stringRedisTemplate.execute(CREATE_STREAM_SCRIPT,keys,"g1","0");
    }
}
